<template>
  <div ref="chart" style="width: 100%; height: 300px"></div>
</template>

<script>
// 引入ECharts主模块
import * as echarts from "echarts";
import {  getStatisticsData } from "@/api/combatUser/importantUser";

export default {
  name: "trendChart",
  props: {
    userId: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      resData: [],
      
    };
  },
  mounted() {
    let id = this.userId;
    let userId111 = this.$route.query.userId;

    this.getStatisticsDataFn(userId111);
  },
  methods: {
    getStatisticsDataFn(userId111) {
      getStatisticsData(userId111).then((res) => {
        if (res.code == 200) {
          //遍历返回为想要的数据格式
          let xData = [];
          let yData = [];
          res.data.forEach((item) => {
            xData.push(item.name);
            yData.push(item.value);
          });
          this.initFn(xData, yData);
        }
      });
    },
    initFn(xData, yData) {
      // 基于准备好的dom，初始化echarts实例
      var myChart = echarts.init(this.$refs.chart);

      // 指定图表的配置项和数据
      var option = {
        title: {
          text: '发文趋势图',
          left: 'center',
          top: 0,
          bottom: 20,
          textStyle: {
            color: '#333',
            fontSize: 16
          }
        },
        color: ["#4285F4", "#00B042"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#6a7985",
              formatter: function (params) {
                return params.value;
              },
            },
          },
          formatter: function (params) {
            let result = params[0].axisValue + "<br/>";
            params.forEach(function (param) {
              result +=
                param.marker + param.seriesName + ": " + param.value + "<br/>";
            });
            return result;
          },
        },

        grid: {
          top: "10%",
          left: "0%",
          right: "0%",
          bottom: "15%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: xData,
            axisLabel: {
              interval: 0,
              textStyle: {
                color: "#333",
              },
            },
            splitNumber: 0,
            axisTick: {
              show: false,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            axisLabel: {
              interval: 0,
              textStyle: {
                color: "#333",
              },
            },
            splitLine: {
              show: true,
              //  改变轴线颜色
              lineStyle: {
                width: 0.5,
                // 使用深浅的间隔色
                color: ["#e0e0e0"],
              },
            },
          },
        ],
        series: [
          {
            name: "发布量",
            type: "line",
            smooth: true,
            lineStyle: {
              width: 0,
            },
            showSymbol: true,
            symbolSize: 5,
            symbol: "circle",
            areaStyle: {
              opacity: 1,
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: "rgba(66, 133, 244,0.6)",
                },
                {
                  offset: 1,
                  color: "rgba(66, 133, 244,0)",
                },
              ]),
            },
            emphasis: {
              focus: "series",
            },
            data: yData,
          },
        ],
      };

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },
  },
};
</script>

<style scoped>
/* 你可以在这里添加一些CSS样式 */
</style>
